package com.example.demo2.controller;



import com.example.demo2.entity.User;
import com.example.demo2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//import org.springframework.web.servlet.ModelAndView;


import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@SessionAttributes("uID")
@CrossOrigin
public class loginController {

    @Autowired
    private UserMapper usermapper;

    private ConcurrentHashMap<String, Integer> concurrentMap;

    @Autowired
    public void MyController(ConcurrentHashMap<String, Integer> concurrentMap) {
        this.concurrentMap = concurrentMap;
    }
    @PostMapping("/VistorLogin")
    public String visterlogin(){
        System.out.println("有游客进行登录");
        concurrentMap.put("uID",-1);
        System.out.println("游客的uID"+concurrentMap.get("uID"));
        return "success";
    }
    @PostMapping("/deletelogin")
    public String deletelogin(){
        System.out.println("退出登录");
        concurrentMap.put("uID",null);
        System.out.println("游客的uID"+concurrentMap.get("uID"));
        return "success";
    }
    @PostMapping("/login")
    public String login(@RequestBody Map<String,String> user) {//输入值为用户名和密码
       //参数传递（json）
       String uName = (String) user.get("username");
        String uPw = (String) user.get("password");
        System.out.println(uName);
        List<User> list = usermapper.findByuNm(uName);
        if(list == null||list.isEmpty()){
            return "用户名或密码错误";
        }
            else{
               if(Objects.equals(list.get(0).getUPw(), uPw)){

                   concurrentMap.put("uID",list.get(0).getUID() );
                if(list.get(0).getURole() == 1){
                    return "zHome.html";//返回到支持人员主页界面
                }
                else return "jHome.html";//返回到检测人员主页界面
            }
            else{
                return "用户名或密码错误";
               }
            }
    }

}
